package com.aries.remoteplus.view;

import com.aries.remoteplus.R;
import com.aries.remoteplus.core.RPConstants;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class RemotePlusActivity extends Activity {
	
	private String ipAddress;
	private String serverName;
	private int tcpPort;
	private int udpPort;
	private String passWord;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
        
        SharedPreferences appPreferences = this.getSharedPreferences(RPConstants.PREFS_IPKEY, 
        										Context.MODE_PRIVATE);

        final Intent serversIntent = new Intent(this, ServersActivity.class);
        if ( appPreferences.getAll().isEmpty() ) 
        {
			this.startActivityForResult(serversIntent, RPConstants.SUC_REQUESTCODE);
		}
        
        Button remotesButton = (Button) this.findViewById(R.id.home_button_remotes);
        Button serversButton = (Button) this.findViewById(R.id.home_button_servers);
        Button prefersButton = (Button) this.findViewById(R.id.home_button_preferences);
        Button infosButton   = (Button) this.findViewById(R.id.home_button_info);
        Button helpButton    = (Button) this.findViewById(R.id.home_button_help);
        Button exitButton    = (Button) this.findViewById(R.id.home_button_exit);
        
        final Intent remotesIntent = new Intent(this, RemotesActivity.class);
        remotesButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				remotesIntent.putExtra("ipAddress", ipAddress);
				remotesIntent.putExtra("serverName", serverName);
				remotesIntent.putExtra("tcpPort", tcpPort);
				remotesIntent.putExtra("udpPort", udpPort);
				remotesIntent.putExtra("passWord", passWord);
				
				startActivity(remotesIntent);
			}
		});
        
        serversButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(serversIntent, RPConstants.SUC_REQUESTCODE);
			}
		});
        
        exitButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	super.onActivityResult(requestCode, resultCode, data);
    	
    	if (requestCode == RPConstants.SUC_REQUESTCODE)
		{
			if (RESULT_OK == resultCode)
			{
				Bundle bundle = data.getExtras();
				
				ipAddress		= bundle.getString("currentIP");
				serverName 		= bundle.getString("currentServerName");
				tcpPort			= bundle.getInt("currentTcpPort");
				udpPort			= bundle.getInt("currentUdpPort");
				passWord		= bundle.getString("currentPassWord");
			}
		}
    }
}